package com.yx.study.leet.primary.tree;

import com.yx.study.leet.utils.TreeNode;

/**
 * 对称二叉树
 * https://leetcode.cn/leetbook/read/top-interview-questions-easy/xn7ihv/
 */
public class IsSymmetric {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetricHelper(root.left, root.right);
    }

    public boolean isSymmetricHelper(TreeNode left, TreeNode right) {
        // 如果左右结点为空
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null || left.val != right.val) {
            // 左右子树 某个为空，或值不对
            return false;
        }
        // 左右子树相同，判断左子树的左孩子结点与右子树的右节点是否一样;
        return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
    }
}
